package com.cd2cd.code_template.code_template.mapper;

import com.cd2cd.code_template.code_template.domain.SysAuth;
import com.cd2cd.code_template.code_template.mapper.gen.SuperSysAuthMapper;
import com.cd2cd.code_template.vo.SysAuthVo;
import com.cd2cd.code_template.vo.SysRouteVo;
import org.apache.ibatis.annotations.*;

import java.util.HashMap;
import java.util.List;

@Mapper
public interface SysAuthMapper extends SuperSysAuthMapper {

    @Select("select * from sys_auth where auth=#{auth}")
    @ResultMap("BaseResultMap")
    List<SysAuth> queryListByAuthVal(@Param("auth") String auth);


    @ResultType(String.class)
    @Select("select * from sys_auth where type = #{type} and auth = 'yes'")
    List<SysAuth> selectByType(@Param("type") String type);

    int insertOrUpdate(SysAuth sysAuth);

    @ResultType(SysRouteVo.class)
    @Select("select guid guid, route_path path,component_name component,type access from sys_auth where auth_type = 'menu' and route_path is not null and component_name is not null")
    List<SysRouteVo> selectAllRoutes();

    @Select("select " +
            "* " +
            "from " +
            "sys_auth " +
            "where guid in " +
            "(" +
            "select " +
            "auth_guid " +
            "from " +
            "sys_relation_auth where `type`='role' and login_type=#{type} and relation_id in " +
            "(select relation_id from sys_relation_user where `type`='role' and login_type=#{type} and user_id = #{userId}) " +
            "union " +
            "select " +
            "auth_guid " +
            "from " +
            "sys_relation_auth where `type`='group' and login_type=#{type} and relation_id in " +
            "(select relation_id from sys_relation_user where `type`='group' and login_type=#{type} and user_id = #{userId}) " +
            ") " +
            "and (auth_type = 'contents' or auth_type = 'menu') and display = 'yes' and route_path is not null")
    @ResultMap("BaseResultMap")
    List<SysAuth> selectMenuByUser(@Param("userId") Integer userId,@Param("type") String type);

    @Select("select * from sys_auth where type = #{type} and (auth_type = 'contents' or auth_type = 'menu') and display = 'yes' and route_path is not null")
    @ResultMap("BaseResultMap")
    List<SysAuth> selectMenuByType(@Param("type") String type);

    long countByMap(HashMap<String, Object> map);

    List<SysAuth> selectByMap(HashMap<String, Object> map);

    @Select("select auth_guid from sys_relation_auth where login_type = 'Backend' and type = #{type} and relation_id = #{relationId}")
    @ResultType(String.class)
    List<String> selectGuid(@Param("relationId") Integer relationId,@Param("type") String type);
}